package com.kl.controller;

import com.alibaba.fastjson.JSONObject;
import com.kl.domain.CollectionDTO;
import com.kl.domain.FreightDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.Collection;
import com.kl.service.ICollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/collect")
@Api(value = "代收货款管理接口")
public class CollectionController {

    @Autowired
    private ICollectionService collectionService;

    @ApiOperation(value = "根据id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "cid", required = true, paramType = "query"),
    })
    @GetMapping
    public Result findById(@RequestParam Long cid) {
        Collection byId = collectionService.getBaseMapper().selectById(cid);
        return Result.build(ConstantEnum.SUCCESS).setResult(byId);
    }

    @ApiOperation(value = "修改代收货款详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collectionDTO", value = "实体类", required = true, paramType = "body"),
    })
    @PutMapping
    public Result update(@RequestBody CollectionDTO collectionDTO) {
        String s = JSONObject.toJSONString(collectionDTO);
        Collection freight = JSONObject.parseObject(s, Collection.class);
        collectionService.updateById(freight);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation(value = "增加代收货款详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collectionDTO", value = "实体类", required = true, paramType = "body")
    })
    @PostMapping
    public Result add(@RequestBody CollectionDTO collectionDTO) {
        //FreightDTO dto = new FreightDTO();
        // freightServiceFeign.add(dto);
        String s = JSONObject.toJSONString(collectionDTO);
        Collection freight = JSONObject.parseObject(s, Collection.class);
        collectionService.save(freight);
        return Result.build(ConstantEnum.SUCCESS);
    }
}
